package com.hmall.cart.config;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
@RequiredArgsConstructor
public class MqConfig {
    private final RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        System.out.println("为mq设置消息回退处理");
        // 设置RabbitMQ发送消息回退处理，比如设置路由错误，那么交换机会将消息退回来
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                System.out.println("returnedMessage.getExchange() = " + returnedMessage.getExchange());
                System.out.println("returnedMessage.getRoutingKey() = " + returnedMessage.getRoutingKey());
                System.out.println("returnedMessage.getMessage() = " + returnedMessage.getMessage());
            }
        });
    }

}
